<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<TITLE>雷达信号实时走势图</TITLE>
<style type="text/css">
	
	#ipInput{
		text-align: center;
		margin-top: 50px;
		width: 150px;  
	    height: 22px; 
	}
	#portInput{
		width: 50px;  
	    height: 22px; 
	}
	#joinButton {  
	    width: 60px;  
	    height: 22px; 
	    padding-top: 8px;
	    background-color: #639ee2;  
	    color: #FFFFFF;  
	    border-radius: 5px;  
	    text-align: center;
	    font-size: 0.5em;  
	    font-weight: bold;  
	    display: inline-block;  
	} 
	#joinButton:hover{
		background-color: #2f7ed8; 
		cursor:pointer;
	}
	.mykey{
		font-size: 1.5em;  
	}
	.myval{
		font-size: 1em;  
		color: red
	}
</style>
<script type="text/javascript" src="../static/scripts/jquery.js"></script>
<script type="text/javascript" src="../static/scripts/highcharts.js"></script>
<script type="text/javascript">
		var socket
		var updataInterval
		var jsonObj 
		var series
		var upInterval
    	function connect() {
            var host = $("#ipInput").val()+ ":" + $("#portInput").val()+ "/"
			socket = new WebSocket(host);
            _self = this
            try {
                socket.onopen = function (msg) {
                	
                	madeInterval()
                    alert("连接成功！")
                };

                socket.onmessage = function (msg) {
                    if (typeof msg.data == "string") {
                        _self.jsonObj = $.parseJSON(msg.data);
                    }
                    else {
                        alert("数据类型有误");
                    }
                };

                socket.onclose = function (msg) { alert("连接失败") };
            }
            catch (ex) {
                log(ex);
            }
        }
        


</script>
<script type="text/javascript">
	
	function GetRandomNum(Min,Max)
	{   
	var Range = Max - Min;   
	var Rand = Math.random();   
	return(Min + Math.round(Rand * Range));   
	} 
	function _upData(){
		socket.send('666')
		if(this.jsonObj   !=   "undefined"){
			$("#ChannelID").text(this.jsonObj.cid)
			$("#MAC").text(this.jsonObj.mac) 
			$("#Num").text(this.jsonObj.num)  
		}

	}
	function getY(){
		if(this.jsonObj   !=   "undefined"){
			return this.jsonObj.si
		}
		return 0
	}

	function setSeries(series){
		this.series = series
	}
	function madeInterval(){
		self.upInterval = setInterval(function() {   
								_upData();                                 
								var x = new Date().getTime(), // current time         
								//y = Math.random();   
								y = getY();                              
								series.addPoint([x, y], true, true); 
							}, 510);
	}
</script>
<script type="text/javascript">


$(function () {                                                                     
$(document).ready(function() {                                                  
	Highcharts.setOptions({                                                     
		global: {                                                               
			useUTC: false                                                       
		}                                                                       
	});                                                                         
																				
	var chart;                                                                  
	$('#container').highcharts({                                                
		chart: {                                                                
			type: 'area',                                                     
			animation: Highcharts.svg, // don't animate in old IE               
			marginRight: 10,                                                    
			events: {                                                           
				load: function() {                                              
																				
					// set up the updating of the chart each second             
					var series = this.series[0]; 
					setSeries(series)

				}                                                               
			}                                                                   
		},                                                                      
		title: {                                                                
			text: '雷达信号实时走势图'                                            
		},                                                                      
		xAxis: {                                                                
			type: 'datetime',                                                   
			tickPixelInterval: 100                                              
		},                                                                      
		yAxis: {                                                                
			title: {                                                            
				text: '雷达信号强度(db)'                                                   
			},                                                                  
			plotLines: [{                                                       
				value: 0,                                                       
				width: 1,                                                       
				color: '#808080'                                                
			}]                                                                  
		},                                                                      
		tooltip: {                                                              
			formatter: function() {                                             
					return '<b>'+ this.series.name +'</b><br/>'+                
					Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
					Highcharts.numberFormat(this.y, 2);                         
			}                                                                   
		},                                                                      
		legend: {                                                               
			enabled: false                                                      
		},                                                                      
		exporting: {                                                            
			enabled: false                                                      
		},                                                                      
		series: [{                                                              
			name: '信号强度',                                                
			data: (function() {                                                 
				// generate an array of random data                             
				var data = [],                                                  
					time = (new Date()).getTime(),                              
					i;                                                          
																				
				for (i = -19; i <= 0; i++) {                                    
					data.push({                                                 
						x: time + i * 1000,                                     
						y: 0                                       
					});                                                         
				}                                                               
				return data;                                                    
			})()                                                                
		}]                                                                      
	});                                                                         
});                                                                             
																				
});               
</script>

</HEAD>
<BODY>
<div id="container" style="width:1000px;height:500px;margin:0 auto;"></div>
<div style="text-align:center;clear:both;">
<span class="mykey">ChannelID:</span>
<span class="myval" id='ChannelID'>--</span>
<span class="mykey">MAC:</span>
<span class="myval" id='MAC'>--:--:--</span>
<span class="mykey">Num:</span>
<span class="myval" id='Num'>--</span>

<div id='inputDiv'>
	<span>数据源：</span>
	<input id="ipInput" value="ws://127.0.0.1"></input>
	<span>:</span>
	<input id="portInput" value="8000"></input> 
	<div id="joinButton" onclick="connect()">连 接</div>  
</div>
</div>
</BODY>
</HTML>
